// HSS.Forms.UserCredentials.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       UserCredentials.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/23/2010
// ----------------------------------------------------------------------------
namespace HSS.Forms
{
	#region Using Directives
	using System;
	using System.Collections.Generic;
	using System.Text;
	using System.Windows.Forms;
	#endregion

	#region UserCredentials
	/// <summary>
	/// A dialog for collecting user credentials. Username can
	/// be excluded and not shown. Includes an optional delegate 
	/// to validate the supplied credentials.
	/// </summary>
	public sealed class UserCredentials : Form
	{
		#region Fields
		private System.Windows.Forms.Label lblUserName;
		private System.Windows.Forms.TextBox txtUserName;
		private System.Windows.Forms.TextBox txtPassword;
		private System.Windows.Forms.Label lblPassword;
		private System.Windows.Forms.TextBox txtConfirm;
		private System.Windows.Forms.Label lblConfirm;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.ComponentModel.IContainer components = null;
		private int standardHeight = 190;
		private int extendedHeight = 250;
		private bool isExtended = true;
		private Panel pnlCredentials;
		private Panel pnlUserName;
		private string exceptionDialogTitle = "User Credentials";
		#endregion

		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		public UserCredentials()
		{
			this.exceptionDialogTitle = Resources.UserCredentials_ExceptionDialogTitle;
			this.InitializeComponent();
			this.IsPasswordRequired = true;
			this.isExtended = true;
			this.Height = extendedHeight;
			this.lblUserName.Visible = true;
			this.txtUserName.Visible = true;
		}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="includeUserName">true to include username (default); false for password only.</param>
		public UserCredentials(bool includeUserName)
		{
			this.exceptionDialogTitle = Resources.UserCredentials_ExceptionDialogTitle;
			this.InitializeComponent();
			this.IsPasswordRequired = true;
			if (includeUserName)
			{
				this.Height = extendedHeight;
				this.isExtended = true;
				this.pnlUserName.Visible = true;
				this.lblUserName.Visible = true;
				this.txtUserName.Visible = true;
			}
			else
			{
				this.Height = standardHeight;
				this.isExtended = false;
				this.pnlUserName.Visible = false;
				this.lblUserName.Visible = false;
				this.txtUserName.Visible = false;
			}
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the UserName
		/// </summary>
		public string UserName
		{
			get { return this.txtUserName.Text; }
			set { this.txtUserName.Text = value; }
		}
		/// <summary>
		/// Gets or sets the Password
		/// </summary>
		public string Password
		{
			get { return this.txtPassword.Text; }
			set { this.txtPassword.Text = value; }
		}
		/// <summary>
		/// Gets or sets whether or not a password is required. Default: true.
		/// </summary>
		public bool IsPasswordRequired { get; set; }
		/// <summary>
		/// Gets or sets the optional Validation delegate.
		/// </summary>
		public Func<UserCredentials, bool> ValidateCredentials
		{
			get;
			set;
		}
		#endregion

		#region Overrides
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
		#endregion

		#region Methods
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UserCredentials));
			this.lblUserName = new System.Windows.Forms.Label();
			this.txtUserName = new System.Windows.Forms.TextBox();
			this.txtPassword = new System.Windows.Forms.TextBox();
			this.lblPassword = new System.Windows.Forms.Label();
			this.txtConfirm = new System.Windows.Forms.TextBox();
			this.lblConfirm = new System.Windows.Forms.Label();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.pnlCredentials = new System.Windows.Forms.Panel();
			this.pnlUserName = new System.Windows.Forms.Panel();
			this.pnlCredentials.SuspendLayout();
			this.pnlUserName.SuspendLayout();
			this.SuspendLayout();
			// 
			// lblUserName
			// 
			this.lblUserName.AutoSize = true;
			this.lblUserName.Location = new System.Drawing.Point(12, 9);
			this.lblUserName.Name = "lblUserName";
			this.lblUserName.Size = new System.Drawing.Size(68, 16);
			this.lblUserName.TabIndex = 0;
			this.lblUserName.Text = "User Name:";
			// 
			// txtUserName
			// 
			this.txtUserName.Location = new System.Drawing.Point(15, 28);
			this.txtUserName.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
			this.txtUserName.Name = "txtUserName";
			this.txtUserName.Size = new System.Drawing.Size(360, 22);
			this.txtUserName.TabIndex = 1;
			// 
			// txtPassword
			// 
			this.txtPassword.Location = new System.Drawing.Point(15, 28);
			this.txtPassword.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.Size = new System.Drawing.Size(360, 22);
			this.txtPassword.TabIndex = 3;
			this.txtPassword.UseSystemPasswordChar = true;
			// 
			// lblPassword
			// 
			this.lblPassword.AutoSize = true;
			this.lblPassword.Location = new System.Drawing.Point(12, 8);
			this.lblPassword.Name = "lblPassword";
			this.lblPassword.Size = new System.Drawing.Size(61, 16);
			this.lblPassword.TabIndex = 2;
			this.lblPassword.Text = "Password:";
			// 
			// txtConfirm
			// 
			this.txtConfirm.Location = new System.Drawing.Point(15, 83);
			this.txtConfirm.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
			this.txtConfirm.Name = "txtConfirm";
			this.txtConfirm.Size = new System.Drawing.Size(360, 22);
			this.txtConfirm.TabIndex = 6;
			this.txtConfirm.UseSystemPasswordChar = true;
			// 
			// lblConfirm
			// 
			this.lblConfirm.AutoSize = true;
			this.lblConfirm.Location = new System.Drawing.Point(12, 63);
			this.lblConfirm.Name = "lblConfirm";
			this.lblConfirm.Size = new System.Drawing.Size(106, 16);
			this.lblConfirm.TabIndex = 5;
			this.lblConfirm.Text = "Confirm Password:";
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(169, 122);
			this.btnOK.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(100, 30);
			this.btnOK.TabIndex = 7;
			this.btnOK.Text = "&OK";
			this.btnOK.UseVisualStyleBackColor = true;
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(279, 122);
			this.btnCancel.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(100, 30);
			this.btnCancel.TabIndex = 8;
			this.btnCancel.Text = "&Cancel";
			this.btnCancel.UseVisualStyleBackColor = true;
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// pnlCredentials
			// 
			this.pnlCredentials.Controls.Add(this.lblPassword);
			this.pnlCredentials.Controls.Add(this.txtPassword);
			this.pnlCredentials.Controls.Add(this.lblConfirm);
			this.pnlCredentials.Controls.Add(this.txtConfirm);
			this.pnlCredentials.Controls.Add(this.btnOK);
			this.pnlCredentials.Controls.Add(this.btnCancel);
			this.pnlCredentials.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pnlCredentials.Location = new System.Drawing.Point(0, 53);
			this.pnlCredentials.Name = "pnlCredentials";
			this.pnlCredentials.Size = new System.Drawing.Size(394, 169);
			this.pnlCredentials.TabIndex = 9;
			// 
			// pnlUserName
			// 
			this.pnlUserName.Controls.Add(this.txtUserName);
			this.pnlUserName.Controls.Add(this.lblUserName);
			this.pnlUserName.Dock = System.Windows.Forms.DockStyle.Top;
			this.pnlUserName.Location = new System.Drawing.Point(0, 0);
			this.pnlUserName.Name = "pnlUserName";
			this.pnlUserName.Size = new System.Drawing.Size(394, 53);
			this.pnlUserName.TabIndex = 0;
			// 
			// UserCredentials
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 16F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(394, 222);
			this.Controls.Add(this.pnlCredentials);
			this.Controls.Add(this.pnlUserName);
			this.Font = new System.Drawing.Font("Microsoft YaHei", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "UserCredentials";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Set Credentials";
			this.pnlCredentials.ResumeLayout(false);
			this.pnlCredentials.PerformLayout();
			this.pnlUserName.ResumeLayout(false);
			this.pnlUserName.PerformLayout();
			this.ResumeLayout(false);

		}
		#endregion

		#region Event Handlers
		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.isExtended && string.IsNullOrEmpty(this.txtUserName.Text))
			{
				MessageBox.Show(this, Resources.UserCredentials_UserNameRequired, this.exceptionDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				this.txtUserName.Focus();
				return;
			}
			if (this.IsPasswordRequired && string.IsNullOrEmpty(this.txtPassword.Text))
			{
				MessageBox.Show(this, Resources.UserCredentials_PasswordRequired, this.exceptionDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				this.txtConfirm.ResetText();
				this.txtPassword.ResetText();
				this.txtPassword.Focus();
				return;
			}
			if (!string.Equals(this.txtPassword.Text, this.txtConfirm.Text, StringComparison.CurrentCulture))
			{
				MessageBox.Show(this, Resources.UserCredentials_PasswordDoNotMatch, this.exceptionDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				this.txtConfirm.ResetText();
				this.txtPassword.ResetText();
				this.txtPassword.Focus();
				return;
			}
			if (null != this.ValidateCredentials)
			{
				if (!this.ValidateCredentials(this))
				{
					MessageBox.Show(this, Resources.UserCredentials_CredentialsNotValid, this.exceptionDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
					this.txtConfirm.ResetText();
					this.txtPassword.ResetText();
					this.txtPassword.Focus();
					return;
				}
			}

			this.DialogResult = DialogResult.OK;
			this.Close();
		}
		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}
		#endregion
	}
	#endregion
}